package com.shop.util.page;

import org.apache.ibatis.session.SqlSession;

import com.shop.util.MyBatisUtil;

/**
 * 
 * @author wenjuanhenxing
 * @version V1.0 PageList.java 2016-2-14 上午10:12:06
 */
public class PageList {
	private Integer currentPage = 0;

	private Integer rowsPerPage = 0;

	/** 当前所处的记录条数，第currentCount条 */
	private Integer currentCount = 0;

	/** 最大页码 */
	private Integer maxPage = 0;

	/** 即总记录数 */
	private Integer maxRowCount = 0;

	public PageList(String className, Integer currentPage, Integer rowsPerPage) {
		this.currentPage = currentPage;
		this.rowsPerPage = rowsPerPage;
		setCurrentCount();
		setMaxRowCount(className);
		setMaxPage();
	}

	public Integer getCurrentCount() {
		return currentCount;
	}

	public void setCurrentCount() {
		this.currentCount = (currentPage - 1) * rowsPerPage;
	}

	public Integer getMaxPage() {
		return maxPage;
	}

	public void setMaxPage() {
		if (maxRowCount % rowsPerPage == 0 && maxRowCount != 0) {
			maxPage = maxRowCount / rowsPerPage;
		} else {
			maxPage = maxRowCount / rowsPerPage + 1;
		}
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getRowsPerPage() {
		return rowsPerPage;
	}

	public void setRowsPerPage(Integer rowsPerPage) {
		this.rowsPerPage = rowsPerPage;
	}

	public Integer getMaxRowCount() {
		return maxRowCount;
	}

	public void setMaxRowCount(String className) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Integer cnt = sqlSession.selectOne("com.shop.dao.mapping." + className
				+ "Mapper.getCount");
		maxRowCount = cnt;
		MyBatisUtil.closeSession(sqlSession);
	}
}
